/**
 * @author Dennis Li
 * @date 2020/9/7 20:23
 */
public class Graph {

    int res;

    public int ans(int W, int[][] R, int A, int B) {
        int[][] graph = new int[W][W];

        for (int[] arr : R) {
            graph[arr[0]][arr[1]] = arr[2];
        }

        dfs(graph, 0, A, B);
        return res;
    }

    public void dfs(int[][] graph, int cost, int A, int B) {
        if (A == B) {
            res = Math.min(res, cost);
            return;
        }

        for (int i = 0; i < graph.length; i++) {
            if (graph[A][i] != 0) {
                dfs(graph, cost + graph[A][i], i, B);
            }
        }
    }

}
